﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ESOL.Common.Entity;
using ESOL.Business.Inv;
using ESOL.Common;

namespace ESOL.WinUI.Inv
{
    public partial class frmManageCheckingPlan : BaseForm.frmBaseMdiBusinessForm
    {
        #region Variable
        private InvCheckingPlanEntity mSelectedCheckingPlan;
        #endregion

        #region Constructor
        public frmManageCheckingPlan()
        {
            InitializeComponent();
        }
        #endregion

        #region Functions
        private void BindComboBox()
        {
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbStatus, "ICP", true);

            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(repositoryItemGridLookUpEdit1, "ICP", true);

        }
        private string GetCondition()
        {
            string condition = "";

            if (string.IsNullOrEmpty(txtCheckingCode.Text) == false)
            {
                condition += " AND A.CHECKING_PLAN_CODE LIKE '%" + txtCheckingCode.Text + "%'";
            }
            if (dtpFrom.EditValue!=null)
            {
                condition += " AND A.FINISH_DATE >= CONVERT(DATETIME,'" + dtpFrom.EditValue + "')";
            }
            if (dtpTo.EditValue!=null)
            {
                condition += " AND A.FINISH_DATE <= CONVERT(DATETIME,'" + dtpTo.EditValue + "')";
            }
            if (txtCheckingPerson.Tag != null && txtCheckingPerson.Tag.GetType() == typeof(MstPersonEntity))
            {
                MstPersonEntity entity = (MstPersonEntity)txtCheckingPerson.Tag;
                condition += " AND A.CHECKING_PERSON = '" + entity.PersonId + "' ";
            }
            if (string.IsNullOrEmpty(cmbStatus.Text) == false && cmbStatus.SelectedValue != null)
            {
                condition += " AND A.STATUS = '" + cmbStatus.SelectedValue.ToString() + "'";
            }
            return condition;
        }
        private void BindDataGrid()
        {
            string condition = GetCondition();
            string rowCount = txtRowCount.Text;
            string orderBy = "A.CHECKING_PLAN_CODE DESC";
            DataTable dt = InvCheckingPlanBs.GetByCondition(condition, orderBy, rowCount);
            gvMain.DataSource = dt;
        }
        private void BindDetailGrid()
        {
            if (mSelectedCheckingPlan == null)
            {
                return;
            }
            string checkingId = mSelectedCheckingPlan.CheckingPlanId;
            if (string.IsNullOrEmpty(checkingId))
            {
                gvDetail.DataSource = null;
                return;
            }
            DataSet ds = InvCheckingPlanItemBs.GetDataSetByCheckingId(checkingId);
            if (ds == null || ds.Tables.Count == 0)
            {
                gvDetail.DataSource = null;
            }
            gvDetail.DataSource = ds.Tables[0];
        }
        private void ResetButtonsEnable()
        {
            btnModify.Enabled = false;
            btnDelete.Enabled = false;
            btnApprove.Enabled = false;
            btnFinished.Enabled = false;
            btnStart.Enabled = false;
            if (mSelectedCheckingPlan == null)
            {
                return;
            }
            if (mSelectedCheckingPlan.Status == "ICP01")//待审核
            {
                btnModify.Enabled = true;
                btnDelete.Enabled = true;
                btnApprove.Enabled = true;
            }
            if (mSelectedCheckingPlan.Status == "ICP02")//已审核
            {
                btnDelete.Enabled = true;
                btnStart.Enabled = true;
            }
            if (mSelectedCheckingPlan.Status == "ICP03")//已拒绝
            {
                btnModify.Enabled = true;
                btnDelete.Enabled = true;
            }
            if (mSelectedCheckingPlan.Status == "ICP04")//盘点中
            {
                btnFinished.Enabled = true;
                btnModify.Enabled = true;
            }
            if (mSelectedCheckingPlan.Status == "ICP05")//已完成
            {
                btnDelete.Enabled = true;
            }
            if (mSelectedCheckingPlan.Status == "ICP06")//已删除
            { }
        }
        #endregion

        private void frmManageCheckingPlan_Load(object sender, EventArgs e)
        {
            BindComboBox();
            BindDataGrid();
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            DataRow dr = gridView1.GetDataRow(e.RowHandle);
            mSelectedCheckingPlan = new InvCheckingPlanEntity(dr);
            BindDetailGrid();
            ResetButtonsEnable();
        }

        protected override void Click_Query()
        {
            BindDataGrid();
        }
        protected override void Click_View()
        {
            if (mSelectedCheckingPlan == null)
            {
                MessageBox.Show("请选择一个盘点计划！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainCheckingPlan frm = new frmMaintainCheckingPlan();
            frm.ThisActionType = ActionType.View;
            frm.SelectedCheckingPlan = mSelectedCheckingPlan;
            frm.ShowDialog();
        }
        protected override void Click_New()
        {
            frmMaintainCheckingPlan frm = new frmMaintainCheckingPlan();
            frm.ThisActionType = ActionType.New;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }
        protected override void Click_Delete()
        {
            if (mSelectedCheckingPlan == null)
            {
                MessageBox.Show("请选择一个盘点计划！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("是否删除选中盘点计划？", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.No)
            {
                return;
            }
            try
            {

                mSelectedCheckingPlan.Status = "ICP06";
                mSelectedCheckingPlan.LastModifiedBy = CommonParameter.CurrentUser.PersonId;
                mSelectedCheckingPlan.LastModifiedDate = DateTime.Now;
                bool successful = InvCheckingPlanBs.UpdateInvCheckingPlan(mSelectedCheckingPlan);
                if (successful)
                {
                    MessageBox.Show("删除成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    BindDataGrid();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        protected override void Click_Approve()
        {
            if (mSelectedCheckingPlan == null)
            {
                MessageBox.Show("请选择一个盘点计划！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            frmMaintainCheckingPlan frm = new frmMaintainCheckingPlan();
            frm.ThisActionType = ActionType.Approve;
            frm.SelectedCheckingPlan = mSelectedCheckingPlan;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }
        protected override void Click_Modify()
        {
            if (mSelectedCheckingPlan == null)
            {
                MessageBox.Show("请选择一个盘点计划！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainCheckingPlan frm = new frmMaintainCheckingPlan();
            frm.ThisActionType = ActionType.Modify;
            frm.SelectedCheckingPlan = mSelectedCheckingPlan;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            if (mSelectedCheckingPlan == null)
            {
                MessageBox.Show("请选择一个盘点计划！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("盘点将锁住库存。\n\n是否开始盘点？", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.No)
            {
                return;
            }
            try
            {

                mSelectedCheckingPlan.Status = "ICP04";
                mSelectedCheckingPlan.LastModifiedBy = CommonParameter.CurrentUser.PersonId;
                mSelectedCheckingPlan.LastModifiedDate = DateTime.Now;
                mSelectedCheckingPlan.BeginDate = System.DateTime.Now;
                bool successful = InvCheckingPlanBs.StartChecking(mSelectedCheckingPlan);
                if (successful)
                {
                    MessageBox.Show("盘点开始！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    BindDataGrid();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void btnFinished_Click(object sender, EventArgs e)
        {
            if (mSelectedCheckingPlan == null)
            {
                MessageBox.Show("请选择一个盘点计划！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainCheckingPlan frm = new frmMaintainCheckingPlan();
            //frm.ThisActionType = ActionType.Modify;
            frm.SelectedCheckingPlan = mSelectedCheckingPlan;
            frm.IsCheckingFinished = true;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }


    }
}
